package JavaSE.d6;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//生产者消费者
public class Test14 {

    public static void main(String[] args) {
        Producter producter = new Producter();
        producter.start();

        Consumer consumer = new Consumer();
        consumer.start();

        Table.getTable();//弄个桌子

        //主线程看蛋糕
        while (true){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Table.look());
        }

    }
}
class Producter extends Thread{
    public static final Object o=new Object();
    @Override
    public void run() {
        while (true){
            Random random = new Random();
            try {
                Thread.sleep(random.nextInt(11)*1000);
                System.out.println(Thread.currentThread().getName()+":小蛋糕来喽！");
                if(Table.look()<10){
                    Table.makeCake();
                }
                if(Table.look()==10){
                    synchronized (o){
                        System.out.println(Thread.currentThread().getName()+":蛋糕满了，开饭！！");
                        synchronized (Consumer.o){
                            Consumer.o.notifyAll();
                            System.out.println("消费者全都醒了");
                        }
                        o.wait();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Consumer extends Thread{
    public static final Object o=new Object();
    @Override
    public void run() {
        while (true){
            Random random = new Random();
            try {
                Thread.sleep(random.nextInt(11)*1000);
                if(Table.look()>0){
                    Table.eatCake();
                    System.out.println(Thread.currentThread().getName()+"吃了一个蛋糕");
                }else {
                    synchronized (o){
                        System.out.println(Thread.currentThread().getName()+":嗝~蛋糕没了");
                        synchronized (Producter.o){
                            Producter.o.notifyAll();
                            System.out.println("生产者开始做蛋糕");
                        }
                        o.wait();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Table{
    private Table(){}
    private static Table table=null;
    public  static synchronized Table getTable(){
        if(table==null){
            table=new Table();
            box=new ArrayList<Cake>();
        }
        return table;
    }
    private static List<Cake> box=null;
    public static void makeCake(){
        box.add(new Cake());
    }
    public static void eatCake(){
        box.remove(box.size()-1);
    }
    public static int look(){
        return box.size();
    }
}
class Cake{
    String name="蛋糕";
}